using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using StockholmsStad.Windows.UI.Infrastructure;
using StockholmsStad.Windows.UI.Infrastructure.Events;
using StockholmsStad.Windows.UI.Modules.Couple.Controllers;
using StockholmsStad.Windows.UI.Modules.Couple.Interfaces;
using StockholmsStad.Windows.UI.Modules.Couple.Presenters;
using StockholmsStad.Windows.UI.Modules.Couple.Services;
using StockholmsStad.Windows.UI.Modules.Couple.Views;
using StockholmsStad.Windows.UI.Modules.Couple.Controls;

namespace StockholmsStad.Windows.UI.Modules.Couple
{
    public class CoupleModule : IModule
    {
        private readonly IUnityContainer container;
        private readonly IRegionManager regionManager;
        private readonly IEventAggregator eventAggregator;

        public CoupleModule(IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.eventAggregator = eventAggregator;
        }

        public void Initialize()
        {
            RegisterViewsAndServices();
        }

        public void LoadView()
        {
            var args = new UIEventArgs { ShellTitle = "Parvis - Paret" };
            eventAggregator.GetEvent<UIEvent>().Publish(args);

            var couplesPresenter = container.Resolve<CoupleSummaryPresenter>();
            var contentRegion = regionManager.Regions[RegionNames.ContentRegion];
            var loadedView = contentRegion.GetView(ViewNames.ListView);

            if (loadedView == null)
                contentRegion.Add(couplesPresenter.View, ViewNames.ListView);
            //else
                contentRegion.Activate(couplesPresenter.View);

            // Load Tabregions
            var couplePresenter = container.Resolve<CoupleDetailsPresenter>();
            var bookingPresenter = container.Resolve<BookingPresenter>();
            var ceremonyPresenter = container.Resolve<CeremonyPresenter>();
            var historyPresenter = container.Resolve<HistoryPresenter>();
            var extraPresenter = container.Resolve<ExtrasPresenter>();
            var lawTextPresenter = container.Resolve<LawTextsPresenter>();

            var tabRegion = regionManager.Regions[RegionNames.TabRegion];

            tabRegion.Add(couplePresenter.View, ViewNames.CoupleTabView);
            tabRegion.Add(bookingPresenter.View, ViewNames.BookingTabView);
            tabRegion.Add(ceremonyPresenter.View, ViewNames.CeremonyTabView);
            tabRegion.Add(historyPresenter.View, ViewNames.HistoryTabView);
            tabRegion.Add(extraPresenter.View, ViewNames.ExtrasTabView);
            tabRegion.Add(lawTextPresenter.View, ViewNames.LawTextsTabView);
        }

        protected void RegisterViewsAndServices()
        {
            container.RegisterType<ICoupleService, CoupleService>(new ContainerControlledLifetimeManager());
            container.RegisterType<IBookingService, BookingService>(new ContainerControlledLifetimeManager());
            container.RegisterType<ICeremonyService, CeremonyService>(new ContainerControlledLifetimeManager());
            container.RegisterType<IHistoryService, HistoryService>(new ContainerControlledLifetimeManager());
            container.RegisterType<IExtrasService, ExtrasService>(new ContainerControlledLifetimeManager());
            container.RegisterType<ILawTextService, LawTextService>(new ContainerControlledLifetimeManager());
            container.RegisterType<ICouplesController, CouplesController>(new ContainerControlledLifetimeManager());

            container.RegisterType<ICoupleSummaryView, CoupleSummaryView>();
            container.RegisterType<ICouplesListView, CouplesListView>();
            container.RegisterType<ICoupleDetailsView, TabItemCoupleView>();
            container.RegisterType<IBookingView, TabItemBookingView>();
            container.RegisterType<ICeremonyView, TabItemCeremonyView>();
            container.RegisterType<IHistoryView, TabItemHistoryView>();
            container.RegisterType<IExtrasView, TabItemExtrasView>();
            container.RegisterType<ILawTextsView, TabItemLawTextsView>();

            container.RegisterType<IOrdinaryBookingView, OrdinaryBookingControl>();
            container.RegisterType<IIndividualBookingView, IndividualBookingControl>();
        }
    }
}
